package com.myworkspace.standard.common.utils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class LocationUtils {
    /**
     * 赤道半径
     */
    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * Description : 通过经纬度获取距离(单位：米)
     * Group :
     *
     * @param origin      出发点
     * @param destination 目的地
     * @return double
     * @author honghh
     * @date 2019/2/13 0013 15:50
     */
    public static double getDistance(String origin, String destination) {
        if (origin == null) {
            System.out.println("出发点 经纬度不可以为空！");
            return 0;
        }
        if (destination == null) {
            System.out.println("目的地 经纬度不可以为空！");
            return 0;
        }
        String[] temp = origin.split(",");
        String[] temp2 = destination.split(",");

        double radLat1 = rad(Double.parseDouble(temp[1]));
        double radLat2 = rad(Double.parseDouble(temp2[1]));
        double a = radLat1 - radLat2;
        double b = rad(Double.parseDouble(temp[0])) - rad(Double.parseDouble(temp2[0]));
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        // 保留两位小数
        s = Math.round(s * 100d) / 100d;
        s = s * 1000;
        return s;
    }

    /**
     * Description : 通过经纬度获取距离(单位：米)
     * Group :
     *
     * @param originLon      出发点经度
     * @param originLat      出发点纬度
     * @param destinationLon 目的地经度
     * @param destinationLat 目的地纬度
     * @return double
     * @author honghh
     * @date 2019/2/15 0015 9:14
     */
    public static double getDistance(String originLon, String originLat, String destinationLon, String destinationLat) {
        if (StringUtils.isEmpty(originLon)) {
            System.out.println("出发点 经度不可以为空！");
            return 0;
        }
        if (StringUtils.isEmpty(originLat)) {
            System.out.println("出发点 纬度不可以为空！");
            return 0;
        }
        if (StringUtils.isEmpty(destinationLon)) {
            System.out.println("目的地 经度不可以为空！");
            return 0;
        }
        if (StringUtils.isEmpty(destinationLat)) {
            System.out.println("目的地 纬度不可以为空！");
            return 0;
        }

        double radLat1 = rad(Double.parseDouble(originLat));
        double radLat2 = rad(Double.parseDouble(destinationLat));
        double a = radLat1 - radLat2;
        double b = rad(Double.parseDouble(originLon)) - rad(Double.parseDouble(destinationLon));
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        // 保留两位小数
        s = Math.round(s * 100d) / 100d;
        s = s * 1000;
        return s;
    }



    /**
     * Description : 地理编码 获取详细地址的经纬度
     * Group :
     *
     * @param address     详细地址
     * @param currentCity 市
     * @return java.lang.String
     * @author honghh
     * @date 2019/2/13 0013 15:53
     */
    public static String getLocationGeocodeGeo(String address, String currentCity) {

        Map<String, String> querys = new HashMap<String, String>();
        querys.put("address", address);
        querys.put("city", currentCity);
//        querys.put("key", APP_CODE_GAODE); // APP_CODE_GAODE 高德秘钥

        try {
//            HttpResponse response = HttpUtils.doGet(QUERY_KEYWD_URL_GAODE_GEOCODE_GEO, "", "GET", null, querys);//QUERY_KEYWD_URL_GAODE_GEOCODE_GEO 高德-地址获取唯一经纬度 : http://restapi.amap.com/v3/geocode/geo
//            String resData = EntityUtils.toString(response.getEntity());
//            JSONObject json = JSONObject.parseObject(resData);
//            if (json == null) {
//                logger.error("列表信息获取失败，关键字：" + address + "城市：" + currentCity);
//                return null;
//            }
//            String status = json.getString("status");
//            if ("1".equals(status)) {
//                JSONArray jsonArray = json.getJSONArray("geocodes");
//                if (jsonArray == null || jsonArray.size() == 0) {
//                    return null;
//                }
//                JSONObject object = jsonArray.getJSONObject(0);
//
//                return object.getString("location");
//            } else {
                return null;
//            }
        } catch (Exception e1) {
//            logger.error("调用接口失败！" + e1.getMessage());
            return null;
        }
    }

    public static void main(String[] args) {
//        String origin = getLocationGeocodeGeo("武林广场", "杭州市");
//        String destination = getLocationGeocodeGeo("浙江省杭州市滨江区滨文路470号", "杭州市");
//        System.out.println(destination);
//        double distance = LocationUtils.getDistance(origin.split(",")[0], origin.split(",")[1], destination.split(",")[0], destination.split(",")[1]);
////        LocationUtils.getDistance()
//        //不足一千米用 m 显示
//        if (distance < 1000) {
//            System.out.println("两地距离: " + new Double(distance).intValue() + "M");
//        } else {
//            System.out.println("两地距离: " + distance / 1000 + "KM");
//        }
//        double distance2 = LocationUtils.getDistance(origin, destination);
//        System.out.println("两地距离2: " + distance2 / 1000 + "KM");
//
//        double dis = LocationUtils.getDistance("120.12026","30.33761","120.124597","30.323823");
//        System.out.println("dis  "+dis);
//        list();

        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher("12341234123");
        System.out.println(matcher.matches());
    }

    public static void list(){
        double  longitude= 116.3424590000D;
        double  latitude= 40.0497810000D;
        //先计算查询点的经纬度范围 place small
        double r = 6371000;//地球半径千米
        double dis = 5;//0.5千米距离
        double dlng =  2 * Math.asin(Math.sin(dis/(2*r))/Math.cos(latitude*Math.PI/180));
        dlng = dlng*180/Math.PI;//角度转为弧度
        double dlat = dis/r;
        dlat = dlat*180/Math.PI;
        double minlat =latitude-dlat;
        double maxlat = latitude+dlat;
        double minlng = longitude -dlng;
        double maxlng = longitude + dlng;
        System.out.println("最大经度"+maxlng);
        System.out.println("最大纬度"+maxlat);
        System.out.println("最小经度"+minlng);
        System.out.println("最小纬度"+minlat);
        System.out.println(getDistance(String.valueOf(maxlng)
                , String.valueOf(maxlat)
                , String.valueOf(minlng)
                , String.valueOf(minlat)));

    }


}

